package com.cskaoyan.service.impl;

import com.cskaoyan.bean.data.PageData;
import com.cskaoyan.mapper.MarketCouponMapper;
import com.cskaoyan.model.*;
import com.cskaoyan.service.CouponService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author Pudding
 * @Date 2024/6/12 16:51
 */
@Service
public class CouponServiceImpl implements CouponService {
    @Autowired
    MarketCouponMapper couponMapper;

    @Override
    public List<Map> list(int page, int limit, boolean isAuthenticated) {
        // 1. 创建当前日期
        Date current = new Date();
        // 2. 构造查询条件
        MarketCouponExample example = new MarketCouponExample();
        if (isAuthenticated) {
            // 2.1 添加条件：有效时间限制，如果是0，则基于领取时间的有效天数days；如果是1，则start_time和end_time是优惠券有效期
            example.createCriteria().andDeletedEqualTo(false)
                    .andTimeTypeEqualTo((short) 0)
                    .andTypeEqualTo((short) 0);
            example.or().andDeletedEqualTo(false)
                    .andStatusEqualTo((short) 0)
                    .andStartTimeLessThanOrEqualTo(current)
                    .andEndTimeGreaterThanOrEqualTo(current)
                    .andTypeEqualTo((short) 0);
            // 2.2 进行排序
            example.setOrderByClause("add_time desc");
            // 2.3 开启分页限制
            PageHelper.startPage(page, limit);
            // 2.4 进行条件查询
            List<MarketCoupon> marketCoupons = couponMapper.selectByExample(example);

            // 3. 将响应体中的单个数据封装到map，最终返回响应结果为List<Map>，通过流来进行操作：将List<MarketCoupon>转换为List<Map>
            List<Map> list = marketCoupons.stream()
                    .map(marketCoupon -> {
                        Map<String, Object> map = new HashMap<>();
                        map.put("days", marketCoupon.getDays());
                        map.put("desc", marketCoupon.getDesc());
                        map.put("discount", marketCoupon.getDiscount());
                        map.put("id", marketCoupon.getId());
                        map.put("min", marketCoupon.getMin());
                        map.put("name", marketCoupon.getName());
                        map.put("tag", marketCoupon.getTag());
                        // 有效时间限制，如果是0，则基于领取时间的有效天数days；如果是1，则start_time和end_time是优惠券有效期
                        if (marketCoupon.getTimeType().intValue() == 1) {
                            map.put("startTime", marketCoupon.getStartTime());
                            map.put("endTime", marketCoupon.getEndTime());
                        }
                        map.put("min", marketCoupon.getMin());

                        return map;
                    }).collect(Collectors.toList());

            return list;
        } else {
            // 2.1 添加条件：有效时间限制，如果是0，则基于领取时间的有效天数days；如果是1，则start_time和end_time是优惠券有效期
            example.createCriteria().andDeletedEqualTo(false)
                    .andTimeTypeEqualTo((short) 0)
                    .andTypeEqualTo((short) 1);
            example.or().andDeletedEqualTo(false)
                    .andStatusEqualTo((short) 0)
                    .andStartTimeLessThanOrEqualTo(current)
                    .andEndTimeGreaterThanOrEqualTo(current)
                    .andTypeEqualTo((short) 1);
            // 2.2 进行排序
            example.setOrderByClause("add_time desc");
            // 2.3 开启分页限制
            PageHelper.startPage(page, limit);
            // 2.4 进行条件查询
            List<MarketCoupon> marketCoupons = couponMapper.selectByExample(example);

            // 3. 将响应体中的单个数据封装到map，最终返回响应结果为List<Map>，通过流来进行操作：将List<MarketCoupon>转换为List<Map>
            List<Map> list = marketCoupons.stream()
                    .map(marketCoupon -> {
                        Map<String, Object> map = new HashMap<>();
                        map.put("days", marketCoupon.getDays());
                        map.put("desc", marketCoupon.getDesc());
                        map.put("discount", marketCoupon.getDiscount());
                        map.put("id", marketCoupon.getId());
                        map.put("min", marketCoupon.getMin());
                        map.put("name", marketCoupon.getName());
                        map.put("tag", marketCoupon.getTag());
                        // 有效时间限制，如果是0，则基于领取时间的有效天数days；如果是1，则start_time和end_time是优惠券有效期
                        if (marketCoupon.getTimeType().intValue() == 1) {
                            map.put("startTime", marketCoupon.getStartTime());
                            map.put("endTime", marketCoupon.getEndTime());
                        }
                        map.put("min", marketCoupon.getMin());

                        return map;
                    }).collect(Collectors.toList());

            return list;
        }
    }

    @Override
    public MarketCoupon findCouponById(Integer couponId) {
        MarketCoupon marketCoupon = couponMapper.selectByPrimaryKey(couponId);
        return marketCoupon;
    }

    @Override
    public PageData getList(Integer page, Integer limit) {
        Date current = new Date();

        // 查出所有用户都可以领取的优惠券
        MarketCouponExample example = new MarketCouponExample();
        example.createCriteria()
                .andDeletedEqualTo(false)
                .andTimeTypeEqualTo((short) 0)
                .andTypeEqualTo((short) 0);
        example.or().andDeletedEqualTo(false)
                .andTimeTypeEqualTo((short) 1)
                .andStartTimeLessThanOrEqualTo(current)
                .andEndTimeGreaterThanOrEqualTo(current)
                .andTypeEqualTo((short) 0);

        example.setOrderByClause("add_time desc");
        // 对数据进行分页
        PageHelper.startPage(page, limit);
        List<MarketCoupon> marketCoupons = couponMapper.selectByExample(example);
        PageInfo pageInfo = new PageInfo(marketCoupons);
        //封装数据
        PageData pageData = new PageData();
        pageData.setPage(page);
        pageData.setPages(pageInfo.getPages());
        pageData.setTotal((int) pageInfo.getTotal());
        pageData.setLimit(limit);
        List<Map> list = marketCoupons.stream().map(marketCoupon -> {
            Map<String, Object> map = new HashMap<>();
            map.put("id", marketCoupon.getId());
            map.put("name", marketCoupon.getName());
            map.put("desc", marketCoupon.getDesc());
            map.put("tag", marketCoupon.getTag());
            map.put("total", marketCoupon.getTotal());
            map.put("discount", marketCoupon.getDiscount());
            map.put("days", marketCoupon.getDays());
            if (marketCoupon.getTimeType().intValue() == 1) {
                map.put("startTime", marketCoupon.getStartTime());
                map.put("endTime", marketCoupon.getEndTime());
            }
            map.put("min", marketCoupon.getMin());
            return map;
        }).collect(Collectors.toList());
        pageData.setList(list);
        return pageData;

    }
}
